package at.jku.novellbedsidedevice;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import at.jku.novellbedsidedevice.datasource.calendar.CalendarDataObject;
import at.jku.novellbedsidedevice.datasource.calendar.CalendarDatasource;
import at.jku.novellbedsidedevice.datasource.calllog.CallLogDataObject;
import at.jku.novellbedsidedevice.datasource.calllog.CallLogDatasource;
import at.jku.novellbedsidedevice.datasource.sleepquality.SleepQualityDatasource;
import at.jku.novellbedsidedevice.service.AccelerometerService;
import at.jku.novellbedsidedevice.service.AlarmService;

public class ActivityListUI extends Activity {

	AlarmUI alarmFragment;

	int hour;
	int minute;

	private Button setAlarmOff;
	private Button setAlarm;

	private TextView sleeping_quality_view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set variables
		final Calendar c = Calendar.getInstance();
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);

		setContentView(R.layout.activity_list_ui);

		// create TextViews
		sleeping_quality_view = (TextView) findViewById(R.id.output_sleeping_quality);
		try {
			SleepQualityDatasource squDS = SleepQualityDatasource.getInstance();
			sleeping_quality_view.setText("Your sleeping quality last messured was " + squDS.getLatestSleepQualityDataObject().getQualityInPercent() + "\n");

		} catch (Exception ex) {
			sleeping_quality_view.setText("Your sleeping quality could not be calculated.");
		}
		final TextView timeline_view = (TextView) findViewById(R.id.output_timeline);
		timeline_view.setMovementMethod(new ScrollingMovementMethod());
		timeline_view.setText(setTimeLineText());

		setAlarmOff = (Button) findViewById(R.id.turn_off_button);
		setAlarmOff.setEnabled(false);

		setAlarmOff.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				stopAlarmService();
			}
		});

		setAlarm = (Button) findViewById(R.id.setAlarmButton);
		setAlarm.setEnabled(true);

		setAlarm.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showAlarmDialog(v);
			}
		});
	}

	private CharSequence setTimeLineText() {
		StringBuffer sb = new StringBuffer("");

		// List with CallLogDS
		CallLogDatasource clDS = new CallLogDatasource(this);
		List<CallLogDataObject> callList = new ArrayList<CallLogDataObject>();
		callList = clDS.getData();

		// List with CalendarDS
		CalendarDatasource cDS = new CalendarDatasource(this);
		List<CalendarDataObject> calList = new ArrayList<CalendarDataObject>();
		calList = cDS.getData();

		int callIndex = 0, calIndex = 0;
		while (callIndex < callList.size() || calIndex < calList.size()) {
			if (callIndex < callList.size() && calIndex < calList.size() && callList.get(callIndex).getDate().before(calList.get(calIndex).getDate())) {
				sb.append(callList.get(callIndex).toString() + "\n");
				callIndex++;
			} else if (callIndex < callList.size() && calIndex < calList.size() && callList.get(callIndex).getDate().after(calList.get(calIndex).getDate())) {
				sb.append(calList.get(calIndex).toString() + "\n");
				sb.append("--------------\n");
				calIndex++;
			} else if (callIndex == callList.size()) {
				sb.append(calList.get(calIndex).toString() + "\n");
				sb.append("--------------\n");
				calIndex++;
			} else {
				sb.append(callList.get(callIndex).toString() + "\n");
				sb.append("--------------\n");
				callIndex++;
			}
		}

		return sb;
	}

	public void showAlarmDialog(View v) {
		alarmFragment = new AlarmUI();
		alarmFragment.show(getFragmentManager(), "timePicker");
	}

	public void stopAlarmService() {
		Intent stopAlarm = new Intent(this, AlarmService.class);
		this.stopService(stopAlarm);

		Intent stopAcc = new Intent(this, AccelerometerService.class);
		this.stopService(stopAcc);

		setAlarmOff.setEnabled(false);
		setAlarm.setEnabled(true);
	}

	private boolean isAlarmServiceRunning() {
		ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
			if (AlarmService.class.getName().equals(service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}

	@Override
	protected void onResume() {
		Log.i("ActivityListUI", "onResume()");
		super.onResume();
		setAlarmOff.setEnabled(isAlarmServiceRunning());
		setAlarm.setEnabled(!isAlarmServiceRunning());
		try {
			SleepQualityDatasource squDS = SleepQualityDatasource.getInstance();
			if (squDS.getAverageSleepQualityInPercent() > 0)
				sleeping_quality_view.setText("Your average sleeping quality was " + squDS.getAverageSleepQualityInPercent() * 100 + "%\n");
			else
				sleeping_quality_view.setText("Your sleeping quality could not be calculated.");

		} catch (Exception ex) {
			sleeping_quality_view.setText("Your sleeping quality could not be calculated.");
		}
	}
}
